package com.lanhai.zhonglai.third;

import com.lanhai.zhonglai.beans.StationDataBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.StationDataTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.utils.Tools;

import java.util.Arrays;
import java.util.List;

/**
 * 注意，这个类是一个特别的存在,只有第三位有用
 * 
 * @author zhengwei
 *
 */
public class POPALD implements IStationProtocol {

	@Override
	public byte[] agvArrived(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = 1;
		/*
		if (type == TransDirectionEnum.OnlyIn) {
			// 机台上料
			buf[2] = 1;
		} else {
			// 机台出料
			buf[4] = 1;
		} 
		*/
		buf[2] = 1;
		buf[4] = 1;
		return buf;
	}

	@Override
	public byte[] applyLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = 1;
		buf[2] = 2;
		buf[4] = 2;
		return buf;
	}

	@Override
	public List<StationDataBean> analyseData(String ip, byte[] buf) {
		StationDataBean obj = new StationDataBean();

		obj.setCmdStr(Tools.byte2HexStr(buf));
		obj.setIp(ip);
		obj.setLine((byte)1);
		
		 
		if (buf[2] == 4 ) {
			// 机台异常
			obj.setEventType(StationDataTypeEnum.Error); 
		}else if (buf[2] == 2) {
			// 机台就序
			obj.setEventType(StationDataTypeEnum.AllowTrans); 
		} else	if (buf[2] == 3) {
			// 允许离开
			obj.setEventType(StationDataTypeEnum.AllowLeave); 
		}else if (buf[2] == 5) {
			// 允许转动
			obj.setEventType(StationDataTypeEnum.Receive); 
		}else	if (buf[2] == 6) {
			// 停止传输
			obj.setEventType(StationDataTypeEnum.Stop); 
		} else {
			obj.setEventType(StationDataTypeEnum.Info); 
			StationMountBean mb = new StationMountBean();
			 
			if (buf[4] == 0) {
				// 机台出料
				mb.setDirectionOut(true);
			} else {
				// 机台进料
				mb.setDirectionOut(false);
			}

			int num = buf[6];
			if (mb.isDirectionOut()) {
				// 传出数量
				mb.setOutNum(num);
				mb.setInNum(0);
				if (num == 10) {
					mb.setAllowOut(true);
				}
				
			} else {
				// 传入数量
				mb.setInNum(num);
				mb.setOutNum(0);
				if (num == 0) {
					mb.setAllowIn(true);
				}
				
			}
			obj.setBean(mb); 
		}
		return Arrays.asList(obj);
	}

	@Override
	public byte[] agvLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = 1;
		buf[2] = 0;
		buf[4] = 0;
		return buf;
	}

	@Override
	public byte[] applyRecieve(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = 1;
		/*
		if (type == TransDirectionEnum.OnlyIn) {
			// 机台上料
			buf[2] = 3;
		} else {
			// 机台出料
			buf[4] = 3;
		}
		*/
		buf[2] = 3;
		buf[4] = 3;
		return buf;
	}
	@Override
	public byte[] transError(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = 1;
		buf[2] = 0;
		buf[4] = 0;
		return buf;
	}
}
